package cn.xspace.wnx.view.chat.chatrow;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.squareup.picasso.Picasso;
import com.xspace.android.xafcommon.util.AppConstant;
import com.xspace.android.xafcommon.util.LogUtil;

import java.io.File;

import cn.xspace.wnx.R;
import cn.xspace.library.util.picture.BitmapTransformation;
import cn.xspace.chatui.bean.MsgRecordEntity;

public class EaseChatRowImage extends EaseChatRowFile {

    protected ImageView imageView;
//    private EMImageMessageBody imgBody;

    public EaseChatRowImage(Context context, MsgRecordEntity message, int position, BaseAdapter adapter) {
        super(context, message, position, adapter);
    }

    @Override
    protected void onInflateView() {
        inflater.inflate(!isMySend() ? R.layout.chat_row_received_picture : R.layout.chat_row_sent_picture, this);
    }

    @Override
    protected void onFindViewById() {
//        percentageView = (TextView) findViewById(R.id.percentage);
//        percentageView.setVisibility(View.GONE);
        imageView = (ImageView) findViewById(R.id.image);
    }

    @Override
    protected String getSaveDir() {
        return AppConstant.IMAGE_ADDRESS;
    }

    @Override
    protected void downloadSuccess(String result) {
//        showImageView(result, imageView, message.mFileEntity.file_url_remote);
    }

    @Override
    protected void downloadFail() {
    }


    /**
     * 通过网络获取图片
     */
//    private void getImageByHttp() {
//        OkHttpManager.getInstance().downloadAsync(message.mFileEntity.file_url_remote, AppConstant.IMAGE_ADDRESS, new OkHttpManager.DataCallBack() {
//            @Override
//            public void requestBefore() {
//            }
//
//            @Override
//            public void requestFailure(Request request, IOException e) {
//                imageView.setImageResource(R.drawable.chat_default_image_error);
//                ChatFileDao.getInstance().updateFileDownLoadStausByTime(message.inserttime, ChatFileEntity.DownloadStatus.DOWNLOAD_FIALED.toNumber());
//            }
//
//            @Override
//            public void requestSuccess(BaseReturnBean result) throws Exception {
//            }
//
//            @Override
//            public void requestSuccess(String result) throws Exception {
////                            imageView.setImageResource();
//                LogUtil.d(TAG, "本地下载路径为" + result);
//                message.mFileEntity.file_download_status = ChatFileEntity.DownloadStatus.DOWNLOAD_COMPLETE.toNumber();
//                //更新数据库状态
//                ChatFileDao.getInstance().updateFileDownLoadStausByTime(message.inserttime, ChatFileEntity.DownloadStatus.DOWNLOAD_COMPLETE.toNumber());
//                ChatFileDao.getInstance().updateFileLocalPath(message.inserttime, result);
//
//            }
//        });
//    }
    @Override
    protected void buildSendMap() {
        super.buildSendMap();
//        sendMap.put("msg_text", message.msg_content);
    }

    @Override
    protected void onSetUpView() {
        String localPath = message.mFileEntity.file_url_local;
        LogUtil.d(TAG, localPath);
//        percentageView.setVisibility(View.GONE);
        if (isMySend()) {
            //我发送的图片
            progressBar.setVisibility(View.GONE);
            if (!new File(localPath).exists()) {
                //本地图片没了
//                getFileByHttp();

                if (!TextUtils.isEmpty(message.mFileEntity.file_url_remote)) {
                    Picasso.with(context)
                            .load(message.mFileEntity.file_url_remote)

//                        .resize(200, 200)
                            .placeholder(R.drawable.chat_default_image)
//                        .centerCrop()
                            .error(R.drawable.chat_default_image_error)
                            .transform(new BitmapTransformation(message.mFileEntity.file_url_remote))
                            .into(imageView);
                } else {
                    imageView.setImageResource(R.drawable.chat_default_image_error);
                }

            } else {
                LogUtil.d(TAG, "加载本地图片");
                //直接加载本地
                Picasso.with(context)
                        .load(new File(localPath))
                        .placeholder(R.drawable.chat_default_image)
//                        .centerCrop()
                        .error(R.drawable.chat_default_image_error)
                        .transform(new BitmapTransformation(localPath))
                        .into(imageView);
//                showImageView(localPath, imageView, message.mFileEntity.file_url_remote);
            }
            handleSendMessage();
        } else {
            progressBar.setVisibility(View.GONE);
            if (!new File(localPath).exists()) {
                LogUtil.d(TAG, "图片还没有下载" + localPath);
                //本地图片没了
//                getFileByHttp();

                if (!TextUtils.isEmpty(message.mFileEntity.file_url_remote)) {
                    Picasso.with(context)
                            .load(message.mFileEntity.file_url_remote)

                            .placeholder(R.drawable.chat_default_image)
//                        .centerCrop()
                            .error(R.drawable.chat_default_image_error)
                            .transform(new BitmapTransformation(message.mFileEntity.file_url_remote))
                            .into(imageView);
                } else {
                    imageView.setImageResource(R.drawable.chat_default_image_error);
                }
            } else {
                LogUtil.d(TAG, "本地存在图片" + localPath);
                Picasso.with(context)
                        .load(new File(localPath))

                        .resize(200, 200)
                        .placeholder(R.drawable.chat_default_image)
//                        .centerCrop()
                        .error(R.drawable.chat_default_image_error)
                        .transform(new BitmapTransformation(message.mFileEntity.file_url_remote))
                        .into(imageView);
            }
        }
    }

    @Override
    protected void onUpdateView() {
        super.onUpdateView();
    }

    @Override
    protected void onBubbleClick() {
        Log.d(TAG, "onBubbleClick");
//        Intent intent = new Intent(context, EaseShowBigImageActivity.class);
//        File file = new File(imgBody.getLocalUrl());
//        if (file.exists()) {
//            Uri uri = Uri.fromFile(file);
//            intent.putExtra("uri", uri);
//        } else {
//            // The local full size pic does not exist yet.
//            // ShowBigImage needs to download it from the server
//            // first
//            intent.putExtra("secret", imgBody.getSecret());
//            intent.putExtra("remotepath", imgBody.getRemoteUrl());
//            intent.putExtra("localUrl", imgBody.getLocalUrl());
//        }
//        if (message != null && message.direct() == EMMessage.Direct.RECEIVE && !message.isAcked()
//                && message.getChatType() == ChatType.Chat) {
//            try {
//                EMClient.getInstance().chatManager().ackMessageRead(message.getFrom(), message.getMsgId());
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//        }
//        context.startActivity(intent);
    }

}
